// components/common/CardShuffling/CardShuffling.js
import {
	dumpFunc
} from '@src/common/dump'
import router from "@src/router";
Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		stackArrays: {
			type: Array
		},
		login: {
			type: Boolean
		},
		height: {
			type: Number
		}
	},
	options: {
		multipleSlots: true,
		addGlobalClass: true
	},
	pageLifetimes: {
		show: function () {
			// 页面被展示
		},
	},
	lifetimes: {
		ready() {
			// console.log('stackArrays', this.data.stackArrays);
			let stackArrays = this.data.stackArrays;
			stackArrays.forEach((item, index) => {
				item.index = index;
				item.active = false;
				if (index == 0) {
					item.swpClass = 'swp-left'
				} else {
					item.swpClass = 'swp-right'
				}
			})

			let app = getApp();
			for (let i = 0; i < stackArrays.length; i++) {
				if (stackArrays[i].pic) {
					stackArrays[i].pic = app.toFullUrl(stackArrays[i].pic)
				}
			}
			this.setData({
				swiperList: stackArrays
			});
			// console.log('swiperList', this.data.swiperList)
			this.automove()
		},
	},
	/**
	 * 组件的初始数据
	 */
	data: {
		startX: 0,
		endX: 0,
		iCenter: 3,
		showArray: null,
		msetInterval: null,
		swiperList: []
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		jumpPage(e) {
			let {
				dump,
				biz
			} = e.currentTarget.dataset.item
			if (this.data.login && dump.path && !['URL', 'URL_KF'].includes(dump.type)) {
				dumpFunc(dump);
			} else if (dump.type == 'URL') {
				router.push({
					name: 'mall/inside-url',
					params: {
						url: dump.url,
						title: this.data.swiperList[0].title
					}
				})
			} else if (dump.type == 'URL_KF') {
				let {
					corpId
				} = JSON.parse(biz.params)
				wx.openCustomerServiceChat({
					extInfo: {
						url: dump.url
					},
					corpId: corpId,
					success(res) { },
					fail(res) {
						wx.showToast({
							title: '当前无客服', //提示文字
							icon: "none",
							duration: 2000, //显示时长
						})
					},
				})
			} else {
				this.triggerEvent('login')
			}
		},
		automove() {
			this.intervelHander && clearInterval(this.intervelHander)
			this.intervelHander = setInterval(() => {
				this.moveLeftorRight(1)
			}, 3000)
		},
		pausemove() {
			this.intervelHander && clearInterval(this.intervelHander)
			this.timeoutHander && clearTimeout(this.timeoutHander)
			this.timeoutHande = setTimeout(() => {
				this.automove()
			}, 1000)
		},
		start: function (e) {
			this.data.startPoint = e.changedTouches[0].pageX;
		},
		end: function (e) {
			let isLeft = 0;
			let endPoint = e.changedTouches[0].pageX;
			isLeft = (endPoint - this.data.startPoint);
			if (isLeft > 20 || isLeft < -20) {
				this.pausemove()
				if (isLeft < 0) {
					this.moveLeftorRight(1);
				}
				if (isLeft > 20) {
					this.moveLeftorRight(0);
				}
			}
		},
		moveLeftorRight: function (idx) {
			let swp = this.data.swiperList;
			let max = swp.length;
			let self = this;
			for (let j = 0; j < max; j++) {
				swp[j].active = true;
			}
			if (idx === 1) {
				if (swp[0] && swp[1]) {
					swp[0].swpClass = 'imgleft1';
					swp[1].swpClass = 'imgleft2';
					if (swp[2]) {
						swp[2].swpClass = 'imgleft3';
					}
					this.setData({
						swiperList: swp
					}, () => {
						swp.push(swp.shift());
						self.setData({
							swiperList: swp
						})
					})
				}
			} else {
				if (swp[1]) {
					swp[max - 1].swpClass = 'imgright1'
					swp[0].swpClass = 'imgright2'
					if (swp[2]) {
						swp[1].swpClass = 'imgright3'
					}
					self.setData({
						swiperList: swp
					}, () => {
						swp.unshift(swp.pop());
						self.setData({
							swiperList: swp
						})
					})
				}
			}
		}

	}
})
